package com.hb.medical_research_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.medical_research_system.common.Results;
import com.hb.medical_research_system.entity.Article;
import com.hb.medical_research_system.service.ArticleService;
import com.hb.medical_research_system.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author hb
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-02-25 11:14:47
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService{
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Results add(String address) {
        Article article = new Article();
        article.setTxtAddress(address);
        articleMapper.insert(article);

        System.out.println();
        System.out.println(article.getArticleId());
        System.out.println();

        return Results.info(Results.SUCCESS,"插入成功");
    }

    /**
     * 根据标题搜索
     * @param title
     * @return
     */
    @Override
    public List<Article> searchByTitle(String title) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Article::getTitle, "%"+title+"%");

        List<Article> articles = articleMapper.selectList(lambdaQueryWrapper);

        return articles;
    }

    /**
     * 根据标题搜索(分页 )
     * @param title
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> searchByTitle2(String title, int page, int pageSize) {
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(title), Article::getTitle,"%"+title+"%" );

        articleService.page(pageInfo, queryWrapper);
        return pageInfo;
    }


    /**
     * 根据作者搜索
     * @param another
     * @return
     */
    @Override
    public List<Article> searchByAnother(String another) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Article::getAnother, "%"+another+"%");

        List<Article> articles = articleMapper.selectList(lambdaQueryWrapper);
        return articles;
    }

    /**
     * 根据作者搜索(分页)
     * @param another
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> searchByAnother2(String another, int page, int pageSize) {
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(another), Article::getAnother,"%"+another+"%" );

        articleService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public List<Article> searchByPeriodical(String periodical) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Article::getPeriodical, "%"+periodical+"%");

        List<Article> articles = articleMapper.selectList(lambdaQueryWrapper);
        return articles;
    }

    /**
     * 根据期刊搜索(分页)
     * @param periodical
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> searchByPeriodical2(String periodical, int page, int pageSize) {
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(periodical), Article::getPeriodical,"%"+periodical+"%" );

        articleService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public List<Article> searchByNativeKeyword(String nativeKeyword) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Article::getNativeKeyword, "%"+nativeKeyword+"%");

        List<Article> articles = articleMapper.selectList(lambdaQueryWrapper);
        return articles;
    }

    /**
     * 根据原生关键字搜索(分页)
     * @param nativeKeyword
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> searchByNativeKeyword2(String nativeKeyword, int page, int pageSize) {
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(nativeKeyword), Article::getNativeKeyword,"%"+nativeKeyword+"%" );

        articleService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public List<Article> searchBysummary(String summary) {
        return null;
    }

    /**
     * 根据摘要搜索(分页)
     * @param summary
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> searchBysummary2(String summary, int page, int pageSize) {
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(summary), Article::getSummary,"%"+summary+"%" );

        articleService.page(pageInfo, queryWrapper);
        return pageInfo;
    }


}




